<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="queryParam.name" placeholder="讲师名" />
      </el-form-item>

      <el-form-item>
        <el-select v-model="queryParam.level" clearable placeholder="讲师头衔">
          <el-option :value="1" label="高级讲师" />
          <el-option :value="2" label="首席讲师" />
        </el-select>
      </el-form-item>

      <el-form-item label="添加时间">
        <el-date-picker
          v-model="queryParam.begin"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="queryParam.end"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getListEntity()"
        >查询</el-button
      >
      <el-button type="default" @click="resetData()">清空</el-button>
      <router-link :to="{ path: '/teacher/save' }">
        <el-button type="primary" icon="el-icon-plus">添加讲师</el-button>
      </router-link>
    </el-form>
    <template>
      <el-table
        :data="info"
        style="width: 100%"
        :default-sort="{ prop: 'date', order: 'descending' }"
      >
        <el-table-column prop="name" label="姓名" width="180">
        </el-table-column>
        <el-table-column prop="career" label="资料"> </el-table-column>
        <el-table-column prop="level" label="简介">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.level === 1 ? 'primary' : 'success'"
              disable-transitions
              >{{ scope.row.level === 1 ? "高级讲师" : "首席讲师" }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="gmtCreate" sortable label="创建时间"> </el-table-column>
        <el-table-column prop="gmtModified" label="修改时间"> </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <router-link :to="{ path: '/teacher/edit/' + scope.row.id }">
              <el-button type="info" round>修改</el-button>
            </router-link>
            <el-button
              type="danger"
              @click="deleteEntityById(scope.row.id)"
              round
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tolal"
      >
      </el-pagination> </template
    >>
  </div>
</template>

<script>
import teacherApi from "@/api/edu/teacher";

export default {
  data() {
    return {
      page: 1,
      limit: 5,
      // 条件查询参数
      queryParam: {
        name: "",
        level: "",
        begin: "",
        end: "",
      },
      tolal: 0,
      info: [],
    };
  },
  created() {},
  mounted() {
    this.getListEntity();
  },

  methods: {
    // 获取所有的数据
    getListEntity() {
      teacherApi
        .getListTeacher(this.queryParam, this.page, this.limit)
        .then((res) => {
          this.info = res.data.info;
          this.tolal = res.data.total;
        })
        .catch((error) => {
          console.log(error);
        });
    },
    // 设置页面大小
    handleSizeChange(val) {
      this.limit = val;
      this.getListEntity();
    },
    // 页面切换
    handleCurrentChange(val) {
      this.page = val;
      this.getListEntity();
    },
    resetData() {
      //清空的方法
      //表单输入项数据清空
      this.queryParam = {};
      //查询所有讲师数据
      this.getListEntity();
    },
    //删除讲师的方法
    deleteEntityById(id) {
      this.$confirm("此操作将永久删除讲师记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //点击确定，执行then方法
          //调用删除的方法
          teacherApi.deleteEntityById(id).then((response) => {
            //删除成功
            //提示信息
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            //回到列表页面
            this.getListEntity();
          });
        })
        //点击取消，执行catch方法
        .catch((error) => {
          this.$message("取消删除");
        });
    },
  },
};
</script>

<style>
</style>